﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Utilities;

namespace GroupPurchase.Controllers
{
    public class CommonController : Controller
    {
        //
        // GET: /Common/

        public ActionResult Index()
        {
            return View();
        }
        //上传图片------------------------------------
        public JsonResult UploadImg(FormCollection form)
        {
            Response.Charset = "UTF-8";

            // 初始化一大堆变量
            HttpPostedFileBase postFile = Request.Files["picdata"];//单个文件,表单文件域name
            string attachdir = "/Upload/Images/";     // 上传文件保存路径，结尾要带/
            //int dirtype = 1;                 // 1:按天存入目录 2:按月存入目录 3:按扩展名存目录  建议使用按天存
            int maxattachsize = 2048;     // 最大上传大小
            //string upext = "txt,rar,zip,jpg,jpeg,gif,png,swf,wmv,avi,wma,mp3,mid";    // 上传扩展名
            //int msgtype = 2;                 //返回上传参数的格式：1，只返回url，2，返回参数数组
            string immediate = Request.QueryString["immediate"];//立即上传模式，仅为演示用
            //byte[] file;                     // 统一转换为byte数组处理
            //string localname = "";
            //string disposition = Request.ServerVariables["HTTP_CONTENT_DISPOSITION"];


            bool i = false; ;
            string filePath = Common.UpLoad(postFile, "img", maxattachsize, attachdir, 1, ref i);
            if (i)
            {
                var data = new { url = filePath, title = Request.Form["pictitle"], state = "SUCCESS" };
                //JsonResult json = new JsonResult { Data = new { url = filePath, title = Request.Form["pictitle"], state = "SUCCESS" } }; 
                return Json(data, JsonRequestBehavior.AllowGet);
                // msg = jsonString("!" + filePath);
            }
            else
            {
                var data = new { url = filePath, title = Request.Form["pictitle"], state = filePath };
                //JsonResult json = new JsonResult { Data = new { url = filePath, title = Request.Form["pictitle"], state = "SUCCESS" } }; 
                return Json(data, JsonRequestBehavior.AllowGet);

            }

        }

        //远程上传图片------------------------------------
        public JsonResult SnapImgUp(FormCollection form)
        {
            Response.Charset = "UTF-8";

            // 初始化一大堆变量
            HttpPostedFileBase postFile = Request.Files["picdata"];//单个文件,表单文件域name
            string attachdir = "/Upload/Images/";     // 上传文件保存路径，结尾要带/
            //int dirtype = 1;                 // 1:按天存入目录 2:按月存入目录 3:按扩展名存目录  建议使用按天存
            int maxattachsize = 2048;     // 最大上传大小
            //string upext = "txt,rar,zip,jpg,jpeg,gif,png,swf,wmv,avi,wma,mp3,mid";    // 上传扩展名
            //int msgtype = 2;                 //返回上传参数的格式：1，只返回url，2，返回参数数组
            string immediate = Request.QueryString["immediate"];//立即上传模式，仅为演示用
            //byte[] file;                     // 统一转换为byte数组处理
            //string localname = "";
            //string disposition = Request.ServerVariables["HTTP_CONTENT_DISPOSITION"];


            bool i = false; ;
            string filePath = Common.UpLoad(postFile, "img", maxattachsize, attachdir, 1, ref i);
            if (i)
            {
                var data = new { url = filePath, title = Request.Form["pictitle"], state = "SUCCESS" };
                //JsonResult json = new JsonResult { Data = new { url = filePath, title = Request.Form["pictitle"], state = "SUCCESS" } }; 
                return Json(data, JsonRequestBehavior.AllowGet);
                // msg = jsonString("!" + filePath);
            }
            else
            {
                var data = new { url = filePath, title = Request.Form["pictitle"], state = filePath };
                //JsonResult json = new JsonResult { Data = new { url = filePath, title = Request.Form["pictitle"], state = "SUCCESS" } }; 
                return Json(data, JsonRequestBehavior.AllowGet);

            }

        }

        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="form"></param>
        public JsonResult UploadFile(FormCollection form)
        {
            Response.Charset = "UTF-8";

            // 初始化一大堆变量
            HttpPostedFileBase postFile = Request.Files["Filedata"];//单个文件,表单文件域name
            string attachdir = "/Upload/File/";     // 上传文件保存路径，结尾要带/
            //int dirtype = 1;                 // 1:按天存入目录 2:按月存入目录 3:按扩展名存目录  建议使用按天存
            int maxattachsize = 100*1024;     // 最大上传大小
            //string upext = "txt,rar,zip,jpg,jpeg,gif,png,swf,wmv,avi,wma,mp3,mid";    // 上传扩展名
            //int msgtype = 2;                 //返回上传参数的格式：1，只返回url，2，返回参数数组
            //byte[] file;                     // 统一转换为byte数组处理
            //string localname = "";
            //string disposition = Request.ServerVariables["HTTP_CONTENT_DISPOSITION"];


            bool i = false; ;
            string filePath = Common.UpLoad(postFile, "", maxattachsize, attachdir, 1, ref i);
            if (i)
            {
                var data = new { url = filePath, title = Request.Form["pictitle"], state = "SUCCESS" };
                //JsonResult json = new JsonResult { Data = new { url = filePath, title = Request.Form["pictitle"], state = "SUCCESS" } }; 
                return Json(data, JsonRequestBehavior.AllowGet);
                // msg = jsonString("!" + filePath);
            }
            else
            {
                var data = new { url = filePath, title = Request.Form["pictitle"], state = filePath };
                //JsonResult json = new JsonResult { Data = new { url = filePath, title = Request.Form["pictitle"], state = "SUCCESS" } }; 
                return Json(data, JsonRequestBehavior.AllowGet);

            }
        }
        //验证码-----------------------------------
        public ActionResult VerifyCode()
        {
            VerifyCode verifyCode = new VerifyCode();
            Session["LoginVC"] = verifyCode.Code;
            byte[] bytes = verifyCode.CreateImageOnPage();
            return File(bytes, @"image/jpeg");
        }
    }
}
